#ifndef _ASY_SOCK2_H_
#define _ASY_SOCK2_H_

#include "SockLite.h"
#include "../os/os_thread.h"
#include "../lock/AutoLock.h"
#include "../serialize/SerialBuffer.h"

class CAsyncSockLite : public CSockLite
{
	CAsyncSockLite (const CAsyncSockLite&);
	const CAsyncSockLite &operator = (const CAsyncSockLite&);

public:
	CAsyncSockLite(void);
	virtual ~CAsyncSockLite(void);

public:
	virtual bool Create();

	virtual void Close();

	virtual bool Connect( const char* lpszHostAddress, UINT nHostPort );

	int  Send(const void* lpBuf, int nBufLen);
	int	 Send(std::list<CSerialBuffer>& lstBuffer);
	int	 Send(CSerialBuffer& buffer);

	bool	IsRun() const { return m_bRun; }

protected:
	virtual int  OnReceive();
	virtual void OnClose();
	virtual void OnRead();
	virtual void OnSend();

protected:
	virtual void Parse();
	virtual bool Initialize();
	virtual void Uninitialize();
	virtual void StartThread();

	static  int  WorkThread( void* pParam );
	virtual void RunStart();
	virtual void RunEnd();
	virtual void Run();

	void	OnHandleSendEvent();
	int 	SendRemainPacket();


protected:

	os_thread_t 	m_thread;

	CCritSection	m_clsCritSection;

	bool		m_bWrite;
	bool		m_bRun;

	// 待发送的数据缓冲
	CSerialBuffer					m_SendBuffer;
	// 待接收的数据缓冲
	CSerialBuffer					m_ReadBuffer;

#ifdef _DEBUG
	int m_nTotalRecvCount;
#endif

};




#endif
